package com.example.workoutjournal;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SetActivity extends Activity {
	public static final String EX_ID = "exerciseID";
	private DAO database;
	String setID;
	String setName;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		String toastText = "";
		super.onCreate(savedInstanceState);
		getActionBar().hide();
		setContentView(R.layout.activity_set);
		
		Intent intent = getIntent();
	    String message = intent.getStringExtra(MyExercises.SET_ID);
	    this.setName = intent.getStringExtra("SET_NAME");
		toastText = intent.getStringExtra("TOAST_TEXT");
		if(toastText != null){
			Toast toast = Toast.makeText(this, toastText, Toast.LENGTH_SHORT);
	        toast.show();
		}
	    setTitle(setName);
	    this.setID = message;
		database = new DAO(this);
		database.open();
		
		final List<Exercise> values = database.getAllExercises(Integer.valueOf(this.setID));
		ArrayAdapter<Exercise> adapter = new ArrayAdapter<Exercise>(this,android.R.layout.simple_list_item_1, values);
		ListView listview = (ListView) findViewById(R.id.listEx);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	      @Override
	      public void onItemClick(AdapterView<?> parent, final View view,
	          int position, long id) {
	    	  Exercise getID = values.get(position);
	    	  Intent intent = new Intent(SetActivity.this, DisplayExercise.class);
	    	  intent.putExtra(EX_ID, getID.getID());
	    	  intent.putExtra("EX_NAME", getID.toString());
	    	  intent.putExtra("SET_ID", setID);
	    	  intent.putExtra("SET_NAME", setName);
	    	  database.close();
	      	  startActivity(intent);
	      }
      });
	    
	    // Create the text view
	    //TextView textView = new TextView(this);
	   // textView.setTextSize(40);
	    //textView.setText(message);

	    // Set the text view as the activity layout
	    //setContentView(textView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.set, menu);
		return true;
	}
	
    public void goToAddEx(View view){
    	Intent intent = new Intent(this, AddExercise.class);
    	intent.putExtra("setID", this.setID);
    	intent.putExtra("SET_NAME", setName);
    	database.close();
    	startActivity(intent);
    }
    
    public void deleteSet(View view){
		database.deleteSet(Integer.valueOf(this.setID));
		Intent intent = new Intent(this, MyExercises.class);
		intent.putExtra("TOAST_TEXT", "Set "+setName+" deleted successfully");
		database.close();
		startActivity(intent);
        
    }

}
